package com.hyb.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hyb.pojo.Bigitems;
import com.hyb.pojo.Info;
import com.hyb.pojo.Smallitems;
import com.hyb.service.BigitemsService;
import com.hyb.service.InfoService;
import com.hyb.service.SmallitemsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class InfoController {
    @Autowired
    InfoService infoService;
    @Autowired
    BigitemsService bigitemsService;
    @Autowired
    SmallitemsService smallitemsService;
    @RequestMapping("/info-list")
    public String toInfoList(@RequestParam(defaultValue = "1",value = "pageNum") Integer pageNum, ModelMap map,Integer m_id,String title){
        PageHelper.startPage(pageNum,10);
        map.addAttribute("m_id",m_id);
        map.addAttribute("title",title);
        List<Info> infoList = infoService.getAll(map);
        PageInfo<Info> pageInfo =new PageInfo<>(infoList);
        map.addAttribute("pageInfo",pageInfo);
        return "/admin/info-list";
    }
    @RequestMapping("/delInfo")
    @ResponseBody
    public String delInfo(@RequestParam int id){
        infoService.deleteById(id);
        return "ok";
    }
    @RequestMapping("/info-add")
    public String toInfoAdd(ModelMap map){
        List<Bigitems> bigList = bigitemsService.getAll();
        map.addAttribute("bigList",bigList);
        return"/admin/info-add";
    }
    @RequestMapping("/addInfo")
    @ResponseBody
    public String addInfo(@RequestParam Map data,ModelMap map){
        String msg="";
        map.addAttribute("m_id",1);
        List<Info> all = infoService.getAll(map);
        for (Info info : all) {
            if(info.getTitle().equals(data.get("title"))){
                msg="此标题已存在";
                return msg;
            }
        }
        Info info1 = new Info();
        info1.setTitle((String) data.get("title"));
        info1.setAuthor((String) data.get("author"));
        info1.setItems_id(Integer.parseInt((String)data.get("small")));
        info1.setContent((String) data.get("content"));
        infoService.addAll(info1);
        msg="ok";
        return msg;
    }
    @RequestMapping("/editInfo")
    @ResponseBody
    public String editInfo(@RequestParam Map data,ModelMap map){
        String msg="";
        map.addAttribute("m_id",1);
        List<Info> infoList = infoService.getAll(map);
        for (Info info1 : infoList) {
            if (info1.getTitle().equals(data.get("title"))){
                msg="此标题已存在";
                return msg;
            }
        }
        Info info1 = new Info();
        Date date1 = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String update_date = sf.format(date1);
        info1.setI_id(Integer.parseInt((String) data.get("i_id")));
        info1.setTitle((String) data.get("title"));
        info1.setAuthor((String) data.get("author"));
        info1.setUpdate_date(update_date);
        info1.setItems_id(Integer.parseInt((String)data.get("small")));
        info1.setContent((String) data.get("content"));
        infoService.updateById(info1);
        msg="ok";
        return msg;
    }
    @RequestMapping("/select")
    @ResponseBody
    public Map select(@RequestParam int id, ModelMap map){
        HashMap map1 = new HashMap();
        List<Smallitems> smallitemsList = smallitemsService.getByBisId(id);
        for (Smallitems smallitems : smallitemsList) {
            map1.put(smallitems.getSi_id(),smallitems.getSi_name());
        }
        return map1;
    }
    @RequestMapping("/delSelectInfo")
    public String delSelectInfo(@RequestParam String[] data){
        for (int i=0;i<data.length;i++){
            infoService.deleteById(Integer.parseInt(data[i]));
        }
        return"redirect:/info-list";
    }
    @RequestMapping("/toInfo-edit")
    public String toAdminEdit(@RequestParam Integer id,ModelMap map){
        Info info = infoService.getById(id);
        Smallitems smallitems = smallitemsService.getById(info.getItems_id());
        List<Smallitems> smallList = smallitemsService.getAll();
        map.addAttribute("info",info);
        map.addAttribute("smallitems",smallitems);
        map.addAttribute("smallList",smallList);
        return "/admin/info-edit";
    }
}
